package com.mzj.eagle.jobs.controller;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;

@RestController
@RequestMapping("/emq")
public class ApkUpdateController {

	private static final Logger LOG = LoggerFactory.getLogger(ApkUpdateController.class);

	@Autowired
	private MqttClient mqttClient;
	
	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(@RequestParam String topic,@RequestParam String message,@RequestParam Integer qos) {
		try {
			if(StringUtils.isEmpty(topic)||StringUtils.isEmpty(message)||
					qos == null) {
				return "The parameter is null!";
			}
			topic = topic.trim();
			message = message.trim();
			byte[] payload = message.getBytes();
			LOG.info("Apk update topic:{},messge:{},qos:{}",topic,message,qos);
			mqttClient.publish(topic, payload, qos, true);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			return "error";
		}
		return "success";
	}

}
